<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>新建文案</title>
    <script type="text/javascript" src="../simditor/scripts/jquery.min.js"></script>
    <script type="text/javascript" src="../simditor/scripts/module.js"></script>
    <script type="text/javascript" src="../simditor/scripts/hotkeys.js"></script>
    <script type="text/javascript" src="../simditor/scripts/simditor.js"></script>
    <link rel="stylesheet" href="../simditor/styles/simditor.css">
    <link rel="stylesheet" href="../layui/css/layui.css" media="all">
    <script src="../layui/layui.js" charset="utf-8"></script>
    <style>
        .layui-unselect {
            width: 120px;
        }

        .layui-form-select {
            width: 120px;
        }

        .simditor {
            width: 400px;
            height: 300px;
        }

        .simditor-body {
            /*开启滚动条*/
            /*overflow-y: scroll !important;*/
            /*设置高度*/
            min-height: 100px;
            max-height: 100px;
        }
    </style>
</head>

<body>
<form class="layui-form" action="">
    <div class="layui-form-item" style="margin-top: 15px">
        <label class="layui-form-label">页面</label>
        <div class="layui-input-block">
            <select name="page" lay-filter="page" id="page" lay-verify="page">
                <option value=""></option>
                <option value="0">首&nbsp;&nbsp;&nbsp;&nbsp;页</option>
                <option value="1">详情页</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item layui-form-text" style="width: 500px;height: 300px">
        <label class="layui-form-label">内容</label>
        <div class="layui-input-block">
            <textarea id="content" class="layui-textarea" autofocus lay-verify="content"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
            <!--<button type="reset" class="layui-btn layui-btn-primary">重置</button>-->
        </div>
    </div>
</form>

</body>

<script>

    var editor = new Simditor({
        textarea: $('#content'),
        placeholder: '请输入内容，所见即所得', // 编辑器默认值（默认为’’）
        toolbar: [
            'title',
            'bold',
            'italic',
            'underline',
            'strikethrough',
            'fontScale',
            'color',
            'ol',         // # ordered list
            'ul',         // # unordered list
            'blockquote',
            'code',      //# code block
            //'table',
            'link',
            //'image',
            'hr',     //# horizontal ruler
            'indent',
            'outdent',
            'alignment'
        ],
        toolbarFloat: true,
        toolbarFloatOffset: 0,
        tabIndent: true,
        //optional options
    });

    layui.use(['form', 'layer'], function () {
        var form = layui.form
            , layer = layui.layer
            , $ = layui.jquery

        //表单验证
        form.verify({
            page: function (value) {
                if (value == null || value == "") {
                    return "请选择使用页面."
                }
            }
            , content: function (value) {
                if (value == null || value == "") {
                    return "请输入内容."
                }
            }
        });

        //监听提交
        form.on('submit(demo1)', function (data) {
            var content = editor.getValue();
            var page = $("#page").val();
            var notice = {
                page: page,
                content: content
            }
            $.ajax({
                url: '/refuse-web/api/notice/addnotice',
                method: 'post',
                contentType: 'application/json; charset=UTF-8',//设置content-type
                data: JSON.stringify(notice),//将对象转换成json字符串
                dataType: 'JSON',
                success:
                    function (res) {
                        if (res.code == '0') {
                            layer.msg(res.message, {icon: 1, time: 1000}, function () {
                                layer.closeAll();
                                parent.location.reload();
                            });
                        } else if (res.code == '204') {
                            layer.msg(res.message, {icon: 5, time: 1000}, function () {
                                layer.closeAll();
                                parent.location.reload();
                            });
                        } else {
                            layer.msg("添加失败.", {icon: 5, time: 1000}, function () {
                                layer.closeAll();
                                parent.location.reload();
                            });
                        }
                    }
                ,
                error: function (data) {
                    layer.msg("服务器忙...", {icon: 5, time: 1000})
                }
            });
            return false;
        })
    });
</script>
</html>